/**
 * Copyleft (C) 2023-2024 maminjie <canpool@163.com>
 * SPDX-License-Identifier: BSD-3-Clause
 **/
#include "maticonbutton.h"
#include "maticonbutton_p.h"
#include "matrippleoverlay.h"
#include "matstyle_p.h"
#include <QEvent>
#include <QPainter>

QX_MATERIAL_BEGIN_NAMESPACE

/*!
 *  \class MatIconButtonPrivate
 *  \internal
 */

MatIconButtonPrivate::MatIconButtonPrivate(MatIconButton *q)
    : q_ptr(q)
{
}

MatIconButtonPrivate::~MatIconButtonPrivate()
{
}

void MatIconButtonPrivate::init()
{
    Q_Q(MatIconButton);

    rippleOverlay = new MatRippleOverlay(q->parentWidget());
    useThemeColors = true;

    rippleOverlay->installEventFilter(q);

    q->setStyle(&MatStyle::instance());

    QSizePolicy policy;
    policy.setWidthForHeight(true);
    q->setSizePolicy(policy);
}

void MatIconButtonPrivate::updateRipple()
{
    Q_Q(MatIconButton);

    QRect r(q->rect());
    r.setSize(QSize(q->width() * 2, q->height() * 2));
    r.moveCenter(q->geometry().center());
    rippleOverlay->setGeometry(r);
}

/*!
 *  \class MatIconButton
 */

MatIconButton::MatIconButton(const QIcon &icon, QWidget *parent)
    : QAbstractButton(parent), d_ptr(new MatIconButtonPrivate(this))
{
    d_func()->init();

    setIcon(icon);
}

MatIconButton::~MatIconButton()
{
}

/*!
 *  \reimp
 */
QSize MatIconButton::sizeHint() const
{
    return iconSize();
}

void MatIconButton::setUseThemeColors(bool value)
{
    Q_D(MatIconButton);

    MATERIAL_SET_THEME_COLORS(value);
    update();
}

bool MatIconButton::useThemeColors() const
{
    Q_D(const MatIconButton);

    return d->useThemeColors;
}

void MatIconButton::setColor(const QColor &color)
{
    Q_D(MatIconButton);

    d->color = color;

    MATERIAL_DISABLE_THEME_COLORS();
    update();
}

QColor MatIconButton::color() const
{
    Q_D(const MatIconButton);

    if (d->useThemeColors || !d->color.isValid()) {
        return MatStyle::instance().themeColor(MK_text);
    }
    return d->color;
}

void MatIconButton::setDisabledColor(const QColor &color)
{
    Q_D(MatIconButton);

    d->disabledColor = color;

    MATERIAL_DISABLE_THEME_COLORS();
    update();
}

QColor MatIconButton::disabledColor() const
{
    Q_D(const MatIconButton);

    if (d->useThemeColors || !d->disabledColor.isValid()) {
        return MatStyle::instance().themeColor(MK_disabled1);
    }
    return d->disabledColor;
}

MatIconButton::MatIconButton(MatIconButtonPrivate &d, QWidget *parent)
    : QAbstractButton(parent), d_ptr(&d)
{
    d_func()->init();
}

/*!
 *  \reimp
 */
bool MatIconButton::event(QEvent *event)
{
    Q_D(MatIconButton);

    switch (event->type()) {
    case QEvent::Move:
    case QEvent::Resize:
        d->updateRipple();
        break;
    case QEvent::ParentChange: {
        QWidget *widget;
        if ((widget = parentWidget())) {
            d->rippleOverlay->setParent(widget);
        }
        break;
    }
    default:
        break;
    }
    return QAbstractButton::event(event);
}

/*!
 *  \reimp
 */
bool MatIconButton::eventFilter(QObject *obj, QEvent *event)
{
    if (QEvent::Resize == event->type()) {
        Q_D(MatIconButton);

        d->updateRipple();
    }
    return QAbstractButton::eventFilter(obj, event);
}

/*!
 *  \reimp
 */
void MatIconButton::mousePressEvent(QMouseEvent *event)
{
    Q_D(MatIconButton);

    d->rippleOverlay->addRipple(QPoint(d->rippleOverlay->width(), d->rippleOverlay->height()) / 2, iconSize().width());
    emit clicked();

    QAbstractButton::mousePressEvent(event);
}

/*!
 *  \reimp
 */
void MatIconButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QPainter painter(this);

    QPixmap pixmap = icon().pixmap(iconSize());
    QPainter icon(&pixmap);
    icon.setCompositionMode(QPainter::CompositionMode_SourceIn);
    icon.fillRect(pixmap.rect(), isEnabled() ? color() : disabledColor());

    QRect r(rect());
    const qreal w = pixmap.width();
    const qreal h = pixmap.height();
    painter.drawPixmap(QRect((r.width() - w) / 2, (r.height() - h) / 2, w, h), pixmap);
}

QX_MATERIAL_END_NAMESPACE
